<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author PR
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" @click="handleHb()" v-if="status == '0'" v-auth="'asd:asdTm:hb'">
          <Icon icon="ant-design:fork-outlined" /> {{ t('合并') }}
        </a-button>
        <a-button type="default" @click="handleTl()" v-if="status == '4'" v-auth="'asd:asdTm:tl'">
          <Icon icon="ant-design:node-expand-outlined" /> {{ t('退料') }}
        </a-button>
        <a-button
          type="default"
          @click="handleExport()"
          v-if="status == '4'"
          v-auth="'asd:asdTm:dc'"
        >
          <Icon icon="ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
      </template>
    </BasicTable>
    <PosList @register="registerPosDrawer" @success="handleSuccess" />

    <!-- 批量退料登记 -->
    <BatchTl @register="registerBatchTl" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsAsdLlAsdTmLlHList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useGlobSetting } from '/@/hooks/setting';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { downloadByUrl } from '/@/utils/file/download';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    asdTmLlListData,
    fileDataUpload,
    handleDataHb,
    barCodeCheck,
    encLl,
  } from '/@/api/asd/ll/asdTmLl';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import PosList from './posList.vue';
  import BatchTl from '/@/views/asd/tl/batchTl.vue';

  const { ctxAdminPath } = useGlobSetting();
  const { t } = useI18n('asd.ll.asdTmLlH');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('领料申请管理'),
  };

  const props = defineProps({
    status: { type: String, default: '0' },
  });

  const status = props.status;

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('发料仓库'),
        field: 'cwhcode',
        component: 'Input',
      },
      {
        label: t('需求仓库'),
        field: 'xqcwhcode',
        component: 'Input',
      },
      {
        label: t('存货编码'),
        field: 'cinvcode',
        component: 'Input',
      },
      {
        label: t('构成比例'),
        field: 'frate',
        component: 'Input',
      },
      {
        label: t('区域'),
        field: 'area',
        component: 'Input',
      },
      {
        label: t('需求时间'),
        field: 'xqDate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'ASD_ROW_STATUS',
          allowClear: true,
          onChange: handleSuccess,
        },
        ifShow: false,
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('单据号'),
      dataIndex: 'djno',
      key: 'a.djno',
      sorter: true,
      width: 110,
      align: 'left',
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 80,
      align: 'center',
      dictType: 'ASD_ROW_STATUS',
    },
    {
      title: t('导出次数'),
      dataIndex: 'printQty',
      key: 'a.printQty',
      sorter: true,
      width: 90,
      align: 'right',
    },
    {
      title: t('存货编码'),
      dataIndex: 'basInv.cinvcode',
      key: 'inv.cinvcode',
      sorter: true,
      width: 140,
      align: 'left',
    },
    {
      title: t('存货代码'),
      dataIndex: 'basInv.cinvaddcode',
      key: 'inv.cinvaddcode',
      sorter: true,
      width: 130,
      align: 'left',
      defaultHidden: true,
    },
    {
      title: t('存货名称'),
      dataIndex: 'basInv.cinvname',
      key: 'inv.cinvname',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('构成比例'),
      dataIndex: 'frate',
      key: 'a.frate',
      sorter: true,
      width: 90,
      align: 'right',
      defaultHidden: true,
    },
    {
      title: t('需求数'),
      dataIndex: 'iqty',
      key: 'a.iqty',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('实发数'),
      dataIndex: 'pickQty',
      key: 'a.pick_Qty',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('区域'),
      dataIndex: 'area',
      key: 'a.area',
      sorter: true,
      width: 60,
      align: 'center',
    },
    {
      title: t('需求时间'),
      dataIndex: 'xqDate',
      key: 'a.xq_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('发料仓库'),
      dataIndex: 'cwhcode',
      key: 'a.cwhcode',
      sorter: true,
      width: 90,
      align: 'left',
    },
    {
      title: t('需求仓库'),
      dataIndex: 'xqcwhcode',
      key: 'a.xqcwhcode',
      sorter: true,
      width: 90,
      align: 'left',
    },
    {
      title: t('选定货位'),
      dataIndex: 'cposcode',
      key: 'a.cposcode',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('备注'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'simple-line-icons:paper-plane',
        color: 'success',
        title: t('选择货位'),
        onClick: handlePosForm.bind(this, record),
        auth: 'asd:ll:asdTmLlH:pos',
        ifShow: record.allowEdit,
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('撤销选择货位'),
        popConfirm: {
          title: t('是否确认撤销选择货位'),
          confirm: handlePosEnc.bind(this, record),
        },
        auth: 'asd:ll:asdTmLlH:pos',
        ifShow: record.allowEdit,
      },
    ],
  };

  const [registerPosDrawer, { openDrawer }] = useDrawer();

  // 批量检验
  const [registerBatchTl, { openDrawer: openDrawerBatchTl }] = useDrawer();

  const [registerTable, { reload, getForm, getSelectRows }] = useTable({
    api: asdTmLlListData,
    beforeFetch: (params) => {
      params['status'] = props.status;
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    ellipsis: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  async function handlePosForm(record: Recordable) {
    openDrawer(true, { id: record.id, cinvcode: record.cinvcode, iqty: record.iqty });
  }

  async function handlePosEnc(record: Recordabl) {
    const res = await encLl({ djno: record.djno });
    showMessage(res.message);
    reload();
  }

  async function handleExport() {
    if (getSelectRows().length) {
      var selIds = ref('');
      getSelectRows().forEach((item) => {
        selIds.value += item.id + ',';
      });

      const res = await fileDataUpload({ selIds: selIds.value });
      if (res.result == 'true') {
        downloadByUrl({ url: ctxAdminPath + '/file/download/' + res.data });
      } else {
        showMessage(res.message);
      }
    } else {
      showMessage('请选择一行数据！！');
    }
  }

  async function handleHb() {
    if (getSelectRows().length && getSelectRows().length >= 2) {
      var selIds = ref('');
      var cinvcode = getSelectRows()[0].cinvcode;
      var flag = true;
      getSelectRows().forEach((item) => {
        if (cinvcode != item.cinvcode) {
          flag = false;
          return;
        }
        selIds.value += item.id + ',';
      });
      if (flag) {
        const res = await handleDataHb({ selIds: selIds.value });
        showMessage(res.message);
        reload();
      } else {
        showMessage('请选择一致的物料进行合并!');
      }
    } else {
      showMessage('请选择两行及以上数据！！');
    }
  }

  async function handleTl() {
    if (getSelectRows().length) {
      var selIds = ref('');
      getSelectRows().forEach((item) => {
        selIds.value += item.id + ',';
      });
      const res = await barCodeCheck({ selIds: selIds.value });
      if (res.result == 'true') {
        openDrawerBatchTl(true, res);
      } else {
        showMessage(res.message);
      }
    } else {
      showMessage('请选择至少一条数据！！');
    }
  }

  function handleSuccess() {
    reload();
  }
</script>
